package com.adopapa.nezhacloud.authc.controller;

import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.adopapa.nezhacloud.authc.domain.ResourceInfo;
import com.adopapa.nezhacloud.authc.domain.ResourceInfo.ResourceType;
import com.adopapa.nezhacloud.authc.service.ResourceInfoService;
import com.adopapa.nezhacloud.common.RestResponse;
import com.adopapa.nezhacloud.common.Tools;
import com.adopapa.nezhacloud.ddd.codegen.SqlGen;
import com.adopapa.nezhacloud.ddd.conf.AppConstants;
import com.adopapa.nezhacloud.ddd.controller.BaseController;

@RestController
@RequestMapping("/authc/resourceinfo")
public class ResourceInfoController extends BaseController {

	@Autowired
	private ResourceInfoService resourceInfoService;

	@SqlGen(name = { "权限管理", "资源管理", "分页查询" }, method = SqlGen.METHOD_GET, path = "/resourceinfos/{pageNo}")
	@GetMapping(path = "/resourceinfos/{pageNo}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getResourceInfoPage(@RequestHeader String authorization, //
			@PathVariable Integer pageNo, //
			@RequestParam(required = false) String resourceName, //
			@RequestParam(required = false) String resourceType, //
			@RequestParam(required = false) String remark, //
			@RequestParam(required = false) Integer pageSize) {

		ResourceInfo resourceInfo = new ResourceInfo();

		resourceInfo.setResourceName(resourceName);
		resourceInfo.setResourceType(ResourceType.getEnum(resourceType));
		resourceInfo.setRemark(remark);

		pageSize = Tools.empty(pageSize) ? AppConstants.DEFAULT_PAGE_SIZE : pageSize;

		Page<ResourceInfo> page = resourceInfoService.getResourceInfoPage(pageNo, pageSize, resourceInfo);

		return RestResponse.ok(page);
	}

	@SqlGen(name = { "权限管理", "资源管理", "单个查询" }, method = SqlGen.METHOD_GET, path = "/resourceinfo/{id}")
	@GetMapping(path = "/resourceinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse getHappyDemo(@RequestHeader String authorization, //
			@PathVariable String id) {
		ResourceInfo resourceInfo = resourceInfoService.getResourceInfoById(id);
		return RestResponse.ok(resourceInfo);
	}

	@SqlGen(name = { "权限管理", "资源管理", "编辑信息" }, method = SqlGen.METHOD_POST, path = "/resourceinfos")
	@PostMapping(path = "/resourceinfos", produces = MediaType.APPLICATION_JSON_VALUE, //
			consumes = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse saveResourceInfo(@RequestHeader String authorization, //
			@RequestBody Map<String, Object> params) {

		String id = getMapValue(params, "id", String.class);

		String resourceName = getMapValue(params, "resourceName", String.class);
		String resourceType = getMapValue(params, "resourceType", String.class);
		String remark = getMapValue(params, "remark", String.class);

		if (Tools.empty(resourceType)) {
			throw new RuntimeException("资源类型不能为空");
		}

		ResourceInfo resourceInfo = null;
		if (Tools.empty(id)) {
			resourceInfo = new ResourceInfo();
//			resourceInfo.setCreateTime(Instant.now());
		} else {
			resourceInfo = resourceInfoService.getResourceInfoById(id);
			if (Tools.empty(resourceInfo)) {
				throw new RuntimeException("指定id对应信息不存在");
			}
		}

		resourceInfo.setResourceName(resourceName);
		resourceInfo.setResourceType(ResourceType.getEnum(resourceType));
		resourceInfo.setRemark(remark);

		resourceInfoService.saveResourceInfo(resourceInfo);

		return RestResponse.ok();

	}

	@SqlGen(name = { "权限管理", "资源管理", "删除信息" }, method = SqlGen.METHOD_DELETE, path = "/resourceinfo/{id}")
	@DeleteMapping(path = "/resourceinfo/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
	public RestResponse deleteResourceInfo(@RequestHeader String authorization, //
			@PathVariable String id) {
		try {
			resourceInfoService.deleteResourceInfo(id);
		} catch (Exception e) {
			logger.error(ExceptionUtils.getStackTrace(e));
		}

		return RestResponse.ok();
	}

}
